#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <tspSolution.hpp>
using namespace std;

/// @brief 由指定字符分割字符串
/// @return 一个vector列表，包含若干字字符串
vector<string> splitString(const string& target, const char& cutter) {
    vector<string> result;
    string currStr = "";
    for (int i = 0; i < target.length(); i++) {
        if (target[i] != cutter) {
            currStr.push_back(target[i]);
        }
        else {
            if (currStr.length() > 0) {
                result.push_back(currStr);
                currStr = "";
            }
        }
    }
    if (currStr.length() > 0) { result.push_back(currStr); }
    return result;
}

/// @brief 从指定文件中读取城市信息
/// @param location 目标文件地址
/// @return 一个vector列表，包含城市信息
vector<city> loadCitiesFromFile(string location) {
    vector<city> cities;
    ifstream file(location);
    if (!file.is_open()) {
        cerr << "File Open Failed : " << location << endl;
        return vector<city>();
        // 异常的文件读取状态
    }
    string line;
    while (getline(file, line)) {
        if (line.length() > 0) {
            vector<string> cityInfo = splitString(line, '/');
            if (cityInfo.size() >= 3) {
                city currCity(stoi(cityInfo[0]), stoi(cityInfo[1]), cityInfo[2]);
                cities.push_back(currCity);
            }
        }
        // 逐行读取并处理
    }
    file.close();
    return cities;
}